package your.packag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;

import your.packag.HistoryActivity.HistoryItemAdapter;

import DAO.MyData;
import DTO.AccountChartDTO;
import DTO.CategoryChartDTO;
import DTO.HistoryDTO;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ManagementActivity extends Activity implements OnClickListener{
	List<AccountChartDTO> account = new ArrayList<AccountChartDTO>();
	List<CategoryChartDTO> category = new ArrayList<CategoryChartDTO>();
	static org.jsoup.nodes.Document doc;
	ListView accountlist;
	ListView categorylist;
	ListView savelist;
	Button addaccount;
	Button addcategory;
	Button deposit;
	Button withdrawal;
	 ProgressDialog dialog;
	 private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        	dialog.dismiss();
	        	LoadUI();
	        	

	        }
		};
		Runnable loadData = new Runnable() {
			
			  
			  public void run() {
				  LoadData();
				  handler.sendEmptyMessage(0);
			  }};

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.management);
       

        dialog = new ProgressDialog(this);
        
		
       
	}
	@Override
	protected void onResume()
	{
		super.onResume();
        dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
        new Thread(loadData).start();
	}

	
	public void LoadUI()
	{
		addaccount =(Button)findViewById(R.id.managementbutton2);
		addcategory =(Button)findViewById(R.id.managementbutton3);
		deposit =(Button)findViewById(R.id.managementbutton4);
		withdrawal =(Button)findViewById(R.id.managementbutton5);
		
		accountlist =(ListView)findViewById(R.id.managementlistView1);
		categorylist =(ListView)findViewById(R.id.managementlistView2);
        accountlist.setAdapter(new AccountItemAdapter(this,
				android.R.layout.simple_list_item_1, account));
        categorylist.setAdapter(new CategoryItemAdapter(this,
				android.R.layout.simple_list_item_1, category));
        addaccount.setOnClickListener(this);
        addcategory.setOnClickListener(this);
        deposit.setOnClickListener(this);
        withdrawal.setOnClickListener(this);
		
	}
	public void LoadData()
	{
		HttpGet httpGet = new HttpGet("http://smartwebvina.com/smartlive/index.php?option=com_mymonney");
		  HttpResponse response;
		try {
			response = LoginActivity.postClient.execute(httpGet);
			HttpEntity httpEntity = response.getEntity();
	        String xml = EntityUtils.toString(httpEntity);
	        doc = Jsoup.parse(xml);
	        MyData data = new MyData();
	        account =data.readAccountChart(doc);
	        category = data.readCategotyChart(doc);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  	
	      //org.jsoup.nodes.Document doc = Jsoup.connect("http://smartwebvina.com/smartlive/").get();
	      

	      
	       
	        
	}
	class AccountItemAdapter extends ArrayAdapter<AccountChartDTO> {
		private List<AccountChartDTO> listaccount;

		AccountItemAdapter(Context context, int textViewResourceId,
				List<AccountChartDTO> listaccount) {
			super(context, textViewResourceId, listaccount);
			this.listaccount = listaccount;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.listitem_statistics, null);
			}

			AccountChartDTO account = listaccount.get(position);
			if (account != null) {
				TextView type = (TextView) view.findViewById(R.id.listitemstatistics1);
				TextView value = (TextView) view.findViewById(R.id.listitemstatistics2);
				type.setText(account.type());
				
				value.setText(""+account.value());

					
				

			}

			return view;

		}
	}
	class CategoryItemAdapter extends ArrayAdapter<CategoryChartDTO> {
		private List<CategoryChartDTO> listcategory;

		CategoryItemAdapter(Context context, int textViewResourceId,
				List<CategoryChartDTO> listcategory) {
			super(context, textViewResourceId, listcategory);
			this.listcategory = listcategory;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.listitem_category, null);
			}

			CategoryChartDTO category = listcategory.get(position);
			if (category != null) {
				TextView type = (TextView) view.findViewById(R.id.listitemcategory1);
				TextView value = (TextView) view.findViewById(R.id.listitemcategory2);
				type.setText(category.type());
				
				value.setText(""+category.value());

					
				

			}

			return view;

		}
	}
	
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (arg0.getId()) {
		case R.id.managementbutton2:
			
			intent = new Intent(this,AddAccountActivity.class);
			startActivity(intent);
			break;
		case R.id.managementbutton3:
			
			intent = new Intent(this,AddCategoryActivity.class);
			startActivity(intent);
			break;
			
		case R.id.managementbutton4:
			
			intent = new Intent(this,DepositActivity.class);
			startActivity(intent);
			break;
		case R.id.managementbutton5:
			
			intent = new Intent(this,WithdrawalActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		
	}


}
